package com.zdkj.subway.check.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdkj.subway.check.entity.SwAuTvmCashboxAmountHierarchyDifference;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmCashboxAmountHierarchyDifferenceMapper;
import com.zdkj.subway.check.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 * 数据协查管理-TVM票款差异协查-TVM系统差异协查-TVM层级差异-分钱箱层级差异
 * TVM票款差异协查-现金层级差异-分钱箱信息 SW_AU_TVM_CASHBOX_AMOUNT_HIERARCHY_DIFFERENCE
 */
@Service
public class SwAuTvmCashboxAmountHierarchyDifferenceService {

    @Resource
    private SwAuTvmCashboxAmountHierarchyDifferenceMapper swAuTvmCashboxAmountHierarchyDifferenceMapper;

    public Result getSwAuTvmCashboxAmountHierarchyDifferenceList(JSONObject jsonObject) {
        //分页
        Integer startPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");

        //线路名称
        String lineCode = jsonObject.getString("lineCode");
        //tvm编号
        String tvmOrder = jsonObject.getString("tvmOrder");
        //站点名称
        String stationCode = jsonObject.getString("stationCode");
        //钱箱编号
        String tvmMoneyOrder = jsonObject.getString("tvmMoneyOrder");

        //收入时间
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");

        Example example = new Example(SwAuTvmCashboxAmountHierarchyDifference.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("lineCode", lineCode);
        }
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("tvmOrder", tvmOrder);
        }
        if (!StringUtils.isEmpty(lineCode)) {
            criteria.andEqualTo("stationCode", stationCode);
        }
        if (!StringUtils.isEmpty(tvmMoneyOrder)) {
            criteria.andEqualTo("tvmMoneyOrder", tvmMoneyOrder);
        }
        if (startTime != null && endTime != null) {
            criteria.andBetween("settleTime",startTime,endTime);
        }
        PageHelper.startPage(startPage,pageSize);
        List<SwAuTvmCashboxAmountHierarchyDifference> swAuTvmCashboxAmountHierarchyDifferences =
                swAuTvmCashboxAmountHierarchyDifferenceMapper.selectByExample(example);
        PageInfo<SwAuTvmCashboxAmountHierarchyDifference> swAuTvmCashboxAmountHierarchyDifferencePageInfo
                = new PageInfo<>(swAuTvmCashboxAmountHierarchyDifferences);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list", swAuTvmCashboxAmountHierarchyDifferences);
        resultJson.put("pageInfo", swAuTvmCashboxAmountHierarchyDifferencePageInfo);
        return new Result(ResultDetailMsg.OK, resultJson);
    }

    /**
     * TVM票款差异协查-现金层级差异-分钱箱信息 单独查询
     * @param jsonObject
     * @return
     */
    public Result getSwAuTvmCashboxAmountHierarchyDifference(JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference =
                swAuTvmCashboxAmountHierarchyDifferenceMapper.selectByPrimaryKey(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("obj",swAuTvmCashboxAmountHierarchyDifference);
        return new Result(ResultDetailMsg.OK,resultJson);
    }

    public Result updateSwAuTvmCashboxAmountHierarchyDifference(JSONObject jsonObject) {
        SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference =
                JSONObject.parseObject(jsonObject.toJSONString(), SwAuTvmCashboxAmountHierarchyDifference.class);
        int i = swAuTvmCashboxAmountHierarchyDifferenceMapper.updateByPrimaryKeySelective(swAuTvmCashboxAmountHierarchyDifference);
        return new Result(ResultDetailMsg.OK);
    }
}
